package com.lwj.controller;

import com.github.pagehelper.PageInfo;
import com.lwj.dao.ProjectTeamMapper;
import com.lwj.dao.UserTeamMapper;
import com.lwj.entity.ProjectTeam;
import com.lwj.entity.Team;
import com.lwj.entity.User;
import com.lwj.model.TeamMemberRes;
import com.lwj.model.TeamRes;
import com.lwj.service.TeamService;
import com.lwj.util.DataRet;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.awt.*;
import java.util.List;

/**团队相关
 * @author linwj
 * @date 2018/5/10 13:28
 */

@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private ProjectTeamMapper projectTeamMapper;

    /**
     * 创建团队
     * @param session
     * @param team
     * @return
     */
    @PostMapping("/create")
    public DataRet<String> createTeam(HttpSession session,Team team){
        User u= (User) session.getAttribute("user");
        return teamService.create(u,team);
    }

    /**
     * 团队邀请成员
     * @param user
     * @param teamId
     * @return
     */
    @PostMapping("/invitation")
    public DataRet<String> invitationMember(User user,int teamId){
        return teamService.invitationMember(user,teamId);
    }

    /**
     * 获取用户所属的所有团队
     * @param session
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/init")
    public PageInfo<TeamRes> getTeamInfo(HttpSession session, @RequestParam(value = "pageNo", defaultValue ="1" )int pageNo, @RequestParam(value = "pageSize",defaultValue ="1" )int pageSize){
        return teamService.get((User)session.getAttribute("user"),pageNo,pageSize);
    }

    /**
     * 获取用户为队长的团队信息
     * @param session
     * @return
     */
    @GetMapping("/leader")
    public DataRet<List<Team>> getUserLeaderTeam(HttpSession session){
        return teamService.getUserLeaderTeam((User)session.getAttribute("user"));
    }

    /**
     * 用户团队状态验证：已加入/未加入
     * @param userId
     * @param teamId
     * @return
     */
    @PostMapping("/validation")
    public DataRet<String> validationTeamStatus(int userId,int teamId){
        return teamService.userTeamValidation(userId,teamId);
    }

    /**
     * 邀请成员
     * @param userId
     * @param teamId
     * @return
     */
    @PostMapping("/inviteUser")
    public DataRet<String> inviteUser(int userId,int teamId){
        return teamService.inviteUser(userId,teamId);
    }

    /**
     * 获取团队中用户信息
     * @param teamId
     * @return
     */
    @PostMapping("/initMemberData")
    public DataRet<List<TeamMemberRes>> getTeamMemberInfo(HttpSession session,int teamId){
        return teamService.getTeamMemberInfo((User)session.getAttribute("user"),teamId);
    }

    @PostMapping("/memberStatus")
    public DataRet<List<TeamMemberRes>> memberStatus(HttpSession session,int projectId){
        ProjectTeam projectTeam=new ProjectTeam();
        projectTeam.setProjectId(projectId);
        projectTeam.setStatus("1");
        List<ProjectTeam>projectTeams=projectTeamMapper.selectByProjectTeam(projectTeam);
        int teamId=0;
        if (projectTeams.size()>0){
            teamId=projectTeams.get(0).getTeamId();
        }
        return teamService.getTeamMemberInfo((User)session.getAttribute("user"),teamId);
    }
}
